<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2022/5/20
 * Time: 16:47
 */
namespace app\index\controller;

use app\middleware\Check;
use think\facade\Db;
use think\facade\Log;

class Exam{

    protected $middleware = [
        Check::class => ['except' 	=> [] ]
    ];



    /**
     * 章节列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function chapter(){
        validateParam('city','city');
        //判断用户是否解锁了
        $info = Db::name('user_unlock_region')
            ->where('user_id',uid())
            ->where('region_name',input('city'))
            ->find();
        if(empty($info)){
            return error('未解锁');
        }
        //判断是否解锁了题库
        if($info['is_unlock_question'] !== 1){
            return error('未解锁');
        }
        //获取章节
        $list = Db::name('chapter')
            ->where('city_',input('city'))
            ->order('cid_ asc')
            ->select();
        return success('章节列表',$list);
    }


    /**
     * 章节获取题目列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function questionListByChapter(){
        validateParam('chapter_id','chapter_id');
        $chapter_id = input('chapter_id');
        //获取章节信息
        $chapter = Db::name('chapter')
            ->where('id_',$chapter_id)
            ->find();
        //判断用户是否解锁了
        $info = Db::name('user_unlock_region')
            ->where('user_id',uid())
            ->where('region_name',$chapter['city_'])
            ->find();
        if(empty($info)){
            return error('未解锁');
        }
        //判断是否解锁了题库
        if($info['is_unlock_question'] !== 1){
            return error('未解锁');
        }
        $list = Db::name('question')
            ->field('id_,no_,type_,answer_true,city_')
            ->where('city_',$chapter['city_'])
            ->where('chapter_',$chapter['cid_'])
            ->order('type_ asc,id_ asc')
            ->select();
        return success('章节获取的题目列表',$list);
    }



    /**
     * 顺序练习
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function questionListByCity(){
        validateParam('city','city');
        $city = input('city');
        //判断用户是否解锁了
        $info = Db::name('user_unlock_region')
            ->where('user_id',uid())
            ->where('region_name',$city)
            ->find();
        if(empty($info)){
            return error('未解锁');
        }
        //判断是否解锁了题库
        if($info['is_unlock_question'] !== 1){
            return error('未解锁');
        }

        $list = Db::name('question')
            ->field('id_,no_,type_,answer_true,city_')
            ->where('city_',$city)
            ->order('type_ asc,id_ asc')
            ->select();
        return success('顺序练习',$list);
    }


    /**
     * 获取题目详情（单个获取）
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function oneQuestionDetail(){
        validateParam('qid','qid');
        $question = Db::name('question')
            ->where('id_',input('qid'))
            ->find();
        //判断用户是否解锁了
        $info = Db::name('user_unlock_region')
            ->where('user_id',uid())
            ->where('region_name',$question['city_'])
            ->find();
        if(empty($info)){
            return error('未解锁');
        }
        //判断是否解锁了题库
        if($info['is_unlock_question'] !== 1){
            return error('未解锁');
        }
        //获取用户是否收藏了该题目
        $collect = Db::name('user_collect')
            ->where('user_id',uid())
            ->where('question_id',input('qid'))
            ->find();
        if(empty($collect)){
            $question['is_collect'] = false;
        }else{
            $question['is_collect'] = true;
        }
        //获取技巧详情
        $skill = Db::name('skill_chapter_question')
            ->field('content_,skill_desc,voice_file')
            ->where('qid',input('qid'))
            ->find();
        if(empty($skill)){
            $skill['content_'] = '此题无技巧解析，需要加强记忆';
            $skill['skill_desc'] = null;
            $skill['voice_file'] = null;
        }
        $question['skill'] = $skill;
        return success('题目详情',$question);
    }


    /**
     * 获取题目详情（多个获取）
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function moreQuestionDetail(){
        validateParam('ids','ids');
        if(count(input('ids')) > 100){
            return error('题目数量限制');
        }
        $questions = Db::name('question')
            ->where('id_','in',input('ids'))
            ->select()->toArray();
        //判断用户是否解锁了
        $info = Db::name('user_unlock_region')
            ->where('user_id',uid())
            ->where('region_name',$questions[0]['city_'])
            ->find();
        if(empty($info)){
            return error('未解锁');
        }
        //判断是否解锁了题库
        if($info['is_unlock_question'] !== 1){
            return error('未解锁');
        }
        //获取题目的收藏状态
        //获取用户是否收藏了该题目
        $collect = Db::name('user_collect')
            ->where('user_id',uid())
            ->where('question_id','in',input('ids'))
            ->column('question_id');
        foreach ($questions as $k=>$v){
            if(in_array($v['id_'],$collect)){
                $questions[$k]['is_collect'] = true;
            }else{
                $questions[$k]['is_collect'] = false;
            }
        }
        return success('题目数据',$questions);
    }






    /**
     * 添加浏览继续（顺序练习，章节练习，语音听题，技巧顺序练习，技巧章节练习）
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function addViewRecord(){
        validateParam('from,type','from,type');
        $param = input();
        if($param['from'] === 'bank'){
            //题库
            if($param['type'] === 'paper'){
                //为顺序练习
                validateParam('city,question_id,question_no','city,question_id,question_no');
                //首先判断记录是否存在
                $record = Db::name('user_view_record')
                    ->where('user_id',uid())
                    ->where('city',$param['city'])
                    ->where('from','bank')
                    ->where('type','paper')
                    ->find();
                if(empty($record)){
                    $data['user_id'] = uid();
                    $data['question_id'] = $param['question_id'];
                    $data['question_no'] = $param['question_no'];
                    $data['type'] = 'paper';
                    $data['from'] = 'bank';
                    $data['city'] = $param['city'];
                    $data['update_at'] = currentTime();
                    $data['create_at'] = currentTime();
                    Db::name('user_view_record')->insert($data);
                }else{
                    //更新题目的id
                    $data['question_id'] = $param['question_id'];
                    $data['question_no'] = $param['question_no'];
                    Db::name('user_view_record')
                        ->where('id',$record['id'])
                        ->update($data);
                }
            }elseif($param['type'] === 'chapter'){
                //章节练习
                validateParam('city,chapter_id,question_id,question_no','city,chapter_id,question_id,question_no');
                //首先判断记录是否存在
                $record = Db::name('user_view_record')
                    ->where('user_id',uid())
                    ->where('city',$param['city'])
                    ->where('from','bank')
                    ->where('type','chapter')
                    ->where('chapter_id',$param['chapter_id'])
                    ->find();
                if(empty($record)){
                    $data['user_id'] = uid();
                    $data['chapter_id'] = $param['chapter_id'];//章节的id
                    $data['question_id'] = $param['question_id'];
                    $data['question_no'] = $param['question_no'];
                    $data['type'] = 'chapter';
                    $data['from'] = 'bank';
                    $data['city'] = $param['city'];
                    $data['update_at'] = currentTime();
                    $data['create_at'] = currentTime();
                    Db::name('user_view_record')->insert($data);
                }else{
                    //更新题目的id
                    $data['question_id'] = $param['question_id'];
                    $data['question_no'] = $param['question_no'];
                    Db::name('user_view_record')
                        ->where('id',$record['id'])
                        ->update($data);
                }
            }else{
                //语音听题类型
                validateParam('city,question_id,question_no','city,question_id,question_no');
                //首先判断记录是否存在
                $record = Db::name('user_view_record')
                    ->where('user_id',uid())
                    ->where('city',$param['city'])
                    ->where('from','bank')
                    ->where('type','voice')
                    ->find();
                if(empty($record)){
                    $data['user_id'] = uid();
                    $data['question_id'] = $param['question_id'];
                    $data['question_no'] = $param['question_no'];
                    $data['type'] = 'voice';
                    $data['from'] = 'bank';
                    $data['city'] = $param['city'];
                    $data['update_at'] = currentTime();
                    $data['create_at'] = currentTime();
                    Db::name('user_view_record')->insert($data);
                }else{
                    //更新题目的id
                    $data['question_id'] = $param['question_id'];
                    $data['question_no'] = $param['question_no'];
                    Db::name('user_view_record')
                        ->where('id',$record['id'])
                        ->update($data);
                }
            }
        }else{
            //技巧
            if($param['type'] === 'paper'){
                //为顺序练习
                validateParam('city,skill_paper_id,question_id,question_no','city,skill_paper_id,question_id,question_no');
                //首先判断记录是否存在
                $record = Db::name('user_view_record')
                    ->where('user_id',uid())
                    ->where('city',$param['city'])
                    ->where('skill_paper_id',$param['skill_paper_id'])
                    ->where('from','skill')
                    ->where('type','paper')
                    ->find();
                if(empty($record)){
                    $data['user_id'] = uid();
                    $data['question_id'] = $param['question_id'];
                    $data['question_no'] = $param['question_no'];
                    $data['skill_paper_id'] = $param['skill_paper_id'];
                    $data['type'] = 'paper';
                    $data['from'] = 'skill';
                    $data['city'] = $param['city'];
                    $data['update_at'] = currentTime();
                    $data['create_at'] = currentTime();
                    Db::name('user_view_record')->insert($data);
                }else{
                    //更新题目的id
                    $data['question_id'] = $param['question_id'];
                    $data['question_no'] = $param['question_no'];
                    Db::name('user_view_record')
                        ->where('id',$record['id'])
                        ->update($data);
                }
            }else{
                //章节练习
                validateParam('city,chapter_id,question_id,question_no','city,chapter_id,question_id,question_no');
                //首先判断记录是否存在
                $record = Db::name('user_view_record')
                    ->where('user_id',uid())
                    ->where('city',$param['city'])
                    ->where('from','skill')
                    ->where('type','chapter')
                    ->where('chapter_id',$param['chapter_id'])
                    ->find();
                if(empty($record)){
                    $data['user_id'] = uid();
                    $data['chapter_id'] = $param['chapter_id'];//章节的id
                    $data['question_id'] = $param['question_id'];
                    $data['question_no'] = $param['question_no'];
                    $data['type'] = 'chapter';
                    $data['from'] = 'skill';
                    $data['city'] = $param['city'];
                    $data['update_at'] = currentTime();
                    $data['create_at'] = currentTime();
                    Db::name('user_view_record')->insert($data);
                }else{
                    //更新题目的id
                    $data['question_id'] = $param['question_id'];
                    $data['question_no'] = $param['question_no'];
                    Db::name('user_view_record')
                        ->where('id',$record['id'])
                        ->update($data);
                }
            }
        }
        return success('添加成功');
    }


    /**
     * 获取浏览记录
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getViewRecord(){
        validateParam('from,type','from,type');
        $param = input();
        if($param['from'] === 'bank'){
            //题库
            if($param['type'] === 'paper'){
                //为顺序练习
                validateParam('city','city');
                $record = Db::name('user_view_record')
                    ->where('user_id',uid())
                    ->where('city',$param['city'])
                    ->where('from','bank')
                    ->where('type','paper')
                    ->find();
                //判断题目是否存在
                $question = Db::name('question')
                    ->where('id_',$record['question_id'])
                    ->find();
                if(empty($question)){
                    $record['question_no'] = 0;
                }
                return success('浏览记录',$record);
            }elseif($param['type'] === 'chapter'){
                //章节练习
                validateParam('city,chapter_id','city,chapter_id');
                $record = Db::name('user_view_record')
                    ->where('user_id',uid())
                    ->where('city',$param['city'])
                    ->where('from','bank')
                    ->where('type','chapter')
                    ->where('chapter_id',$param['chapter_id'])
                    ->find();
                //判断题目是否存在
                $question = Db::name('question')
                    ->where('id_',$record['question_id'])
                    ->find();
                if(empty($question)){
                    $record['question_no'] = 0;
                }
                return success('浏览记录',$record);
            }else{
                //语音听题类型
                validateParam('city','city');
                $record = Db::name('user_view_record')
                    ->where('user_id',uid())
                    ->where('city',$param['city'])
                    ->where('from','bank')
                    ->where('type','voice')
                    ->find();
                //判断题目是否存在
                $question = Db::name('question')
                    ->where('id_',$record['question_id'])
                    ->find();
                if(empty($question)){
                    $record['question_no'] = 0;
                }
                return success('浏览记录',$record);
            }
        }else{
            //技巧
            if($param['type'] === 'paper'){
                //为顺序练习
                validateParam('city,skill_paper_id','city,skill_paper_id');
                $record = Db::name('user_view_record')
                    ->where('user_id',uid())
                    ->where('city',$param['city'])
                    ->where('skill_paper_id',$param['skill_paper_id'])
                    ->where('from','skill')
                    ->where('type','paper')
                    ->find();
                //判断题目是否存在
                $question = Db::name('question')
                    ->where('id_',$record['question_id'])
                    ->find();
                if(empty($question)){
                    $record['question_no'] = 0;
                }
                return success('浏览记录',$record);
            }else{
                //章节练习
                validateParam('city,chapter_id','city,chapter_id');
                $record = Db::name('user_view_record')
                    ->where('user_id',uid())
                    ->where('city',$param['city'])
                    ->where('from','skill')
                    ->where('type','chapter')
                    ->where('chapter_id',$param['chapter_id'])
                    ->find();
                //判断题目是否存在
                $question = Db::name('question')
                    ->where('id_',$record['question_id'])
                    ->find();
                if(empty($question)){
                    $record['question_no'] = 0;
                }
                return success('浏览记录',$record);
            }
        }
    }



    /**
     * 添加答题记录（顺序练习，章节练习，语音听题，技巧顺序练习，技巧章节练习）
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function addAnswerRecord(){
        validateParam('from,type','from,type');
        $param = input();
        if($param['from'] === 'bank'){
            //题库
            if($param['type'] === 'paper'){
                //为顺序练习
                validateParam('city,list','city,list');
                //首先判断记录是否存在
                $record = Db::name('user_answer_record')
                    ->where('user_id',uid())
                    ->where('city',$param['city'])
                    ->where('from','bank')
                    ->where('type','paper')
                    ->find();
                if(empty($record)){
                    $data['user_id'] = uid();
                    $data['type'] = 'paper';
                    $data['from'] = 'bank';
                    $data['city'] = $param['city'];
                    $data['list'] = json_encode($param['list']);
                    $data['update_at'] = currentTime();
                    $data['create_at'] = currentTime();
                    Db::name('user_answer_record')->insert($data);
                }else{
                    //更新题目的id
                    $data['list'] = json_encode($param['list']);
                    $data['create_at'] = currentTime();
                    Db::name('user_answer_record')
                        ->where('id',$record['id'])
                        ->update($data);
                }
            }elseif($param['type'] === 'chapter'){
                //章节练习
                validateParam('city,chapter_id,list','city,chapter_id,list');
                //首先判断记录是否存在
                $record = Db::name('user_answer_record')
                    ->where('user_id',uid())
                    ->where('city',$param['city'])
                    ->where('from','bank')
                    ->where('type','chapter')
                    ->where('chapter_id',$param['chapter_id'])
                    ->find();
                if(empty($record)){
                    $data['user_id'] = uid();
                    $data['chapter_id'] = $param['chapter_id'];//章节的id
                    $data['type'] = 'chapter';
                    $data['from'] = 'bank';
                    $data['city'] = $param['city'];
                    $data['list'] = json_encode($param['list']);
                    $data['update_at'] = currentTime();
                    $data['create_at'] = currentTime();
                    Db::name('user_answer_record')->insert($data);
                }else{
                    //更新题目的id
                    $data['list'] = json_encode($param['list']);
                    $data['create_at'] = currentTime();
                    Db::name('user_answer_record')
                        ->where('id',$record['id'])
                        ->update($data);
                }
            }else{
                //语音听题类型
                validateParam('city,list','city,list');
                //首先判断记录是否存在
                $record = Db::name('user_answer_record')
                    ->where('user_id',uid())
                    ->where('city',$param['city'])
                    ->where('from','bank')
                    ->where('type','voice')
                    ->find();
                if(empty($record)){
                    $data['user_id'] = uid();
                    $data['type'] = 'voice';
                    $data['from'] = 'bank';
                    $data['city'] = $param['city'];
                    $data['list'] = json_encode($param['list']);
                    $data['update_at'] = currentTime();
                    $data['create_at'] = currentTime();
                    Db::name('user_answer_record')->insert($data);
                }else{
                    //更新题目的id
                    $data['list'] = json_encode($param['list']);
                    $data['create_at'] = currentTime();
                    Db::name('user_answer_record')
                        ->where('id',$record['id'])
                        ->update($data);
                }
            }
        }else{
            //技巧
            if($param['type'] === 'paper'){
                //为顺序练习
                validateParam('city,list','city,list');
                //首先判断记录是否存在
                $record = Db::name('user_answer_record')
                    ->where('user_id',uid())
                    ->where('city',$param['city'])
                    ->where('from','skill')
                    ->where('type','paper')
                    ->find();
                if(empty($record)){
                    $data['user_id'] = uid();
                    $data['type'] = 'paper';
                    $data['from'] = 'skill';
                    $data['city'] = $param['city'];
                    $data['list'] = json_encode($param['list']);
                    $data['update_at'] = currentTime();
                    $data['create_at'] = currentTime();
                    Db::name('user_answer_record')->insert($data);
                }else{
                    //更新题目的id
                    $data['list'] = json_encode($param['list']);
                    $data['create_at'] = currentTime();
                    Db::name('user_answer_record')
                        ->where('id',$record['id'])
                        ->update($data);
                }
            }else{
                //章节练习
                validateParam('city,chapter_id,list','city,chapter_id,list');
                //首先判断记录是否存在
                $record = Db::name('user_answer_record')
                    ->where('user_id',uid())
                    ->where('city',$param['city'])
                    ->where('from','skill')
                    ->where('type','chapter')
                    ->where('chapter_id',$param['chapter_id'])
                    ->find();
                if(empty($record)){
                    $data['user_id'] = uid();
                    $data['chapter_id'] = $param['chapter_id'];//章节的id
                    $data['type'] = 'chapter';
                    $data['from'] = 'skill';
                    $data['city'] = $param['city'];
                    $data['list'] = json_encode($param['list']);
                    $data['update_at'] = currentTime();
                    $data['create_at'] = currentTime();
                    Db::name('user_answer_record')->insert($data);
                }else{
                    //更新题目的id
                    $data['list'] = json_encode($param['list']);
                    $data['create_at'] = currentTime();
                    Db::name('user_answer_record')
                        ->where('id',$record['id'])
                        ->update($data);
                }
            }
        }
        return success('添加成功');
    }


    /**
     * 获取答题记录
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAnswerRecord(){
        validateParam('from,type','from,type');
        $param = input();
        if($param['from'] === 'bank'){
            //题库
            if($param['type'] === 'paper'){
                //为顺序练习
                validateParam('city','city');
                $record = Db::name('user_answer_record')
                    ->where('user_id',uid())
                    ->where('city',$param['city'])
                    ->where('from','bank')
                    ->where('type','paper')
                    ->find();
                if(!empty($record)) {
                    $record['list'] = json_decode($record['list'],true);
                }
                return success('浏览记录',$record);
            }elseif($param['type'] === 'chapter'){
                //章节练习
                validateParam('city,chapter_id','city,chapter_id');
                $record = Db::name('user_answer_record')
                    ->where('user_id',uid())
                    ->where('city',$param['city'])
                    ->where('from','bank')
                    ->where('type','chapter')
                    ->where('chapter_id',$param['chapter_id'])
                    ->find();
                if(!empty($record)) {
                    $record['list'] = json_decode($record['list'],true);
                }
                return success('浏览记录',$record);
            }else{
                //语音听题类型
                validateParam('city','city');
                $record = Db::name('user_answer_record')
                    ->where('user_id',uid())
                    ->where('city',$param['city'])
                    ->where('from','bank')
                    ->where('type','voice')
                    ->find();
                if(!empty($record)) {
                    $record['list'] = json_decode($record['list'],true);
                }
                return success('浏览记录',$record);
            }
        }else{
            //技巧
            if($param['type'] === 'paper'){
                //为顺序练习
                validateParam('city','city');
                $record = Db::name('user_answer_record')
                    ->where('user_id',uid())
                    ->where('city',$param['city'])
                    ->where('from','skill')
                    ->where('type','paper')
                    ->find();
                if(!empty($record)) {
                    $record['list'] = json_decode($record['list'],true);
                }
                return success('浏览记录',$record);
            }else{
                //章节练习
                validateParam('city,chapter_id','city,chapter_id');
                $record = Db::name('user_answer_record')
                    ->where('user_id',uid())
                    ->where('city',$param['city'])
                    ->where('from','skill')
                    ->where('type','chapter')
                    ->where('chapter_id',$param['chapter_id'])
                    ->find();
                if(!empty($record)) {
                    $record['list'] = json_decode($record['list'],true);
                }
                return success('浏览记录',$record);
            }
        }
    }



    /**
     * 添加错题记录
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function addErrorRecord(){
        validateParam('question_id,city','question_id,city');
        $param = input();
        //首先查询是否存在错题记录
        $error_record=Db::name('user_answer_error')
            ->where('user_id',uid())
            ->where('question_id',$param['question_id'])
            ->where('city',$param['city'])
            ->find();
        if(empty($error_record)) {
            //说明答题错误，增加错题记录
            $error['user_id'] = uid();
            $error['question_id'] = $param['question_id'];
            $error['city'] = $param['city'];
            $error['create_at'] = currentTime();
            Db::name('user_answer_error')->insert($error);
        }
        return success('添加错题记录成功');
    }


    /**
     * 收藏和取消收藏
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function collect(){
        validateParam('question_id,city','question_id,city');
        $param = input();
        if (isset($param['city']) && !is_numeric($param['city'])) {
            $oneCity = Db::name('region')
                ->where('region_name',$param['city'])
                ->find();
            $param['city'] = $oneCity['region_id'] ?? 0;
        }
        //首先判断题目是否已经收藏了
        $collect = Db::name('user_collect')
            ->where('user_id',uid())
            ->where('city',$param['city'])
            ->where('question_id',$param['question_id'])
            ->find();
        if(empty($collect)){
            //收藏题目
            $data['user_id'] = uid();
            $data['city'] = $param['city'];
            $data['question_id'] = $param['question_id'];
            $data['create_at'] = currentTime();
            Db::name('user_collect')->insert($data);
            return success('收藏成功');
        }else{
            //取消收藏
            Db::name('user_collect')
                ->where('id',$collect['id'])
                ->delete();
            return success('取消收藏成功');
        }
    }


    /**
     * 错题数量
     * @return \think\response\Json
     */
    public function errorCount(){
        validateParam('city','city');
        $count = Db::name('user_answer_error')
            ->alias('a')
            ->join('question b','a.question_id=b.id_')
            ->where('a.city',input('city'))
            ->where('a.user_id',uid())
            ->count();
        return success('错题数量',$count);
    }


    /**
     * 错题记录
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function errorList(){
        validateParam('city','city');
        $list = Db::name('user_answer_error')
            ->field('a.*')
            ->alias('a')
            ->join('question b','a.question_id=b.id_')
            ->where('a.city',input('city'))
            ->where('a.user_id',uid())
            ->order('a.create_at desc')
            ->select();
        return success('错题记录',$list);
    }


    /**
     * 收藏数量
     * @return \think\response\Json
     */
    public function collectCount(){
        validateParam('city','city');
        if (is_numeric(input('city'))) {
            $cityId = input('city');
        } else {
            $cityId = Db::name('region')->where('region_name', input('city'))->value('region_id');
        }
        $count = Db::name('user_collect')
            ->alias('a')
            ->join('question b','a.question_id=b.id_')
            ->where('a.city',$cityId)
            ->where('a.user_id',uid())
            ->count();
        return success('收藏数量',$count);
    }


    /**
     * 收藏记录
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function collectList(){
        validateParam('city','city');
        if (is_numeric(input('city'))) {
            $cityId = input('city');
        } else {
            $cityId = Db::name('region')->where('region_name', input('city'))->value('region_id');
        }
        $list = Db::name('user_collect')
            ->field('a.*')
            ->alias('a')
            ->join('question b','a.question_id=b.id_')
            ->where('a.city', $cityId)
            ->where('a.user_id',uid())
            ->select();
        return success('收藏记录',$list);
    }



    /**
     * 清空全部错题
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function clearErrorQuestion(){
        validateParam('city','city');
        $city=input('city');

        Db::name('user_answer_error')
            ->where('user_id',uid())
            ->where('city',$city)
            ->delete();
        return success('清空全部错题成功');
    }




    /**
     * 创建模拟开始
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function mockExam(){
        validateParam('city','city');
        //从试卷中随机50到题目
        $questions = Db::name('question')
            ->where('city_',input('city'))
            ->orderRand()
            ->limit(50)
            ->select();
        //创建模拟考试试卷
        $data['user_id'] = uid();
        $data['city'] = input('city');//关联的城市
        $data['questions'] = json_encode($questions);
        $data['create_at'] = currentTime();
        $mock_exam_id = Db::name('test_mock_exam')->insertGetId($data);
        return success('模拟考试数据',['mock_exam_id'=>$mock_exam_id,'questions'=>$questions]);
    }




    /**
     * 开始考试
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function startMockExam(){
        validateParam('mock_exam_id','mock_exam_id');//模拟考试的id
        Db::name('test_mock_exam')
            ->where('id',input('mock_exam_id'))
            ->where('user_id',uid())
            ->update(['start_exam_time'=>currentTime()]);
        return success('开始考试成功');
    }




    /**
     * 交卷
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function submitMockExam(){
        validateParam('mock_exam_id,answer','mock_exam_id,answer');
        $param = input();
        $test_mock_exam = Db::name('test_mock_exam')
            ->where('id',$param['mock_exam_id'])
            ->where('user_id',uid())
            ->find();
        if(empty($test_mock_exam)){
            return error('模拟考试不存在');
        }
        $success_number = 0;
        $error_number = 0;
        foreach ($param['answer'] as $k=>$v){
            if(isset($v['user_answer'])) {
                if ($v['answer_true'] === $v['user_answer'] || $v['iscorrect'] === true){
                    $success_number++;//答对了
                }else{
                    $error_number++;//答错了
                }
            }else{
                //说明没有答题，直接错误了
                $error_number++;//没有选择答案，未答题
            }
        }
        $data['answer_correct_number'] = $success_number;
        $data['answer_mistake_number'] = $error_number;
        $data['end_exam_time'] = currentTime();
        $data['mock_exam_answer'] = json_encode($param['answer']);
        $data['score'] = $success_number;

        Db::name('test_mock_exam')
            ->where('id',$param['mock_exam_id'])
            ->update($data);
        //计算考试用时
        $exam_time = time() - strtotime($test_mock_exam['start_exam_time']);
        return success('交卷成功',['score'=>$success_number,'answer_correct_number'=>$success_number,'answer_mistake_number'=>$error_number,'exam_time'=>$exam_time]);
    }
}